package com.chenxin.wms.controller;

import com.chenxin.common.annotation.Log;
import com.chenxin.common.core.controller.BaseController;
import com.chenxin.common.core.domain.AjaxResult;
import com.chenxin.common.core.domain.ProcResult;
import com.chenxin.common.core.page.TableDataInfo;
import com.chenxin.common.enums.BusinessType;
import com.chenxin.common.utils.StringUtils;
import com.chenxin.common.utils.poi.ExcelUtil;
import com.chenxin.wms.domain.vo.IbMasterVO;
import com.chenxin.wms.excel.IbMasterWipExcel;
import com.chenxin.wms.process.inbound.IIbMasterImportProcess;
import com.chenxin.wms.service.IIbMasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 入库主单Controller
 *
 * @author gry
 * @date 2025-04-07
 */
@RestController
@RequestMapping("/wms/ibMaster")
public class IbMasterController extends BaseController {
    @Autowired
    private IIbMasterService ibMasterService;
    @Autowired
    private IIbMasterImportProcess ibMasterImportProcess;

    /**
     * 查询入库主单列表
     */
    @PreAuthorize("@ss.hasPermi('wms:ibMaster:list')")
    @GetMapping("/list")
    public TableDataInfo list(IbMasterVO ibMasterVo) {
        startPage();
        List<IbMasterVO> list = ibMasterService.selectIbMasterList(ibMasterVo);
        return getDataTable(list);
    }

    /**
     * 获取入库主单详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms:ibMaster:query')")
    @GetMapping(value = {"/", "/{ibMasterId}"})
    public AjaxResult getInfo(@PathVariable(value = "ibMasterId", required = false) Long ibMasterId) {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(ibMasterId)) {
            IbMasterVO ibMasterVo = ibMasterService.selectIbMasterById(ibMasterId);
            ajax.put(AjaxResult.DATA_TAG, ibMasterVo);
        }
        return ajax;
    }

    /**
     * 废弃入库主单
     */
    @PreAuthorize("@ss.hasPermi('wms:ibMaster:remove')")
    @Log(title = "入库主单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ibMasterIds}")
    public AjaxResult remove(@PathVariable Long[] ibMasterIds) {
        return success("单据废弃成功！");
    }

    @PreAuthorize("@ss.hasPermi('wms:ibMaster:import')")
    @PostMapping("/wip/import")
    public AjaxResult importExcel(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<IbMasterWipExcel> excelUtil = new ExcelUtil<>(IbMasterWipExcel.class);
        List<IbMasterWipExcel> infoList = excelUtil.importExcel(file.getInputStream());
        if (infoList.size() == 0) {
            return error("导入的文件中不存在数据！");
        }
        // 调用单据写入逻辑处理文件内容
        ProcResult result = ibMasterImportProcess.wipImport(infoList, updateSupport, getUserId());
        return success(result.getMessage());
    }

    /**
     * 导出入库主单列表
     */
    @PreAuthorize("@ss.hasPermi('wms:ibMaster:export')")
    @Log(title = "入库主单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IbMasterVO ibMasterVo) {
        List<IbMasterVO> list = ibMasterService.selectIbMasterList(ibMasterVo);
        ExcelUtil<IbMasterVO> util = new ExcelUtil<>(IbMasterVO.class);
        util.exportExcel(response, list, "入库主单数据");
    }
}
